<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/7/27
 * Time: 14:19
 */

namespace app\api\controller;



use app\model\UserModel;
use think\facade\Request;
use think\facade\Validate;
use think\facade\View;

class User
{
    //获取用户列表
    public function index()
    {
        // 查找分类数据
        $params = Request::param();
        $nickname = $params['nickname'] ?? '';
        $model = UserModel::where('user_id', '>', 0);
        if($nickname){
            $model->where("nickname","like",'%'.$nickname.'%');
        }
        $userList =$model->select();
        $data = [
            'status' => 0,
            'message' => '',
            'data' => [
                'userList' => $userList,
            ],
        ];
        return json($data);
    }
  public function add(){
        // 1. 获取参数
        // 2. 校验参数
        // 3. 验证文本是否合规。
        // 4. 写入数据库
        // 5. 返回数据


        //order增加员工
        $params = Request::param();
        $validate = Validate::rule([
            'open_id|用户id' => 'require',
            'nickname|昵称' => 'require',
            'avatar|头像' => 'require' ,
//            'staff_id|员工id' => 'require'
        ]);
        if (!$validate->check($params)) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }

//        if (!$params['staff_id']){
//            $params['staff_id']=null;
//        }
        $user=UserModel::where("open_id","=",$params['open_id'])->find();
        if (!$user){
            $insertData=$params;
//            $insertData['open_id']=$params['open_id'];
            $insertData['add_time'] = $insertData['update_time'] = time();
            $result = UserModel::create($insertData);
            if ($result) {
                $data = [
                    'status' => 0,
                    'message' => '',
                    'data' => [
//                    'result' => true
                        'user' => $result->toArray()
                    ]
                ];
                return json($data);
            }
        }else{
            $user['update_time']=time();
            $result=$user->save();
            $data = [
                'status' => $result ? 0 : 10001,
                'message' => $result ? '' : '修改数据库失败',
                'data' => [
                    'user' =>$result ? true : false
                ]
            ];
            return json($data);
        }



    }
    
    //总用户数接口
    public function chart_people()
    {
        $month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
        //一个月时间戳
        $times = 2592000;
        //2021年1月时间戳
        $ones = 1609430400;
        $result = [];
        foreach ($month as $item => $value) {
            $sales = UserModel::where('add_time', 'between', [$ones, $ones + $times])
                ->select();
            $result[] = $sales->count();
            $ones = $ones + $times;
        }
        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' => $result ? $result : ''
            ]
        ];
        return json($data);
    }
    
    public function openId(){
        $code=Request::param('code');
        $a = file_get_contents("https://api.weixin.qq.com/sns/jscode2session?appid=wx16bfe4d17b6ca738&secret=8ca5848c3274699964f175b697aa7427&js_code=".$code."&grant_type=authorization_code");
        $re = json_decode($a,true);
        if ($re) {
            $data = [
                'status' => 0,
                'message' => '',
                'data' => [
                    'openid' => $re
                ]
            ];
            return json($data);
        }
    }
}